package GetClass;

import java.util.Objects;

public class equalsTest {
    public static void main(String[] args) {
        Student student = new Student("tom",2,"男",99D);
        Student student1 = new Student("tom",2,"男",99D);
        Student student2 = new Student("tom",2,"男",99D);
        Student student3 = new Student("tom",2,"男",99D);
        System.out.println(student.equals(student));//true
        System.out.println(student1.equals(student1));//true
        System.out.println(student2.equals(student2));//true
        System.out.println(student1==student2);//比较的是地址  false
        //字符串String类型equals与==结果上是不一样,字符串对Objecr类中独有的方法就行覆盖(比较内容)
        System.out.println(student1.equals(student3));//false没重写之前的/
        System.out.println(student1.equals(student3));//比较内容重写之后为true
    }
}
class Student{
    String name;
    int age;
    String sex;
    double score;

    public Student(String name, int age, String sex, double score) {
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.score = score;
    }
//重写之后比较内容  1.比较俩个对象的地址是否一样
    //2.确认对象不能为null
//3.确认类型
    //4.转型
    //5.比较内容
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;//比较俩个对象的地址是否一样
        if (o == null || getClass() != o.getClass()) return false;//确认类型
        Student student = (Student) o;//比较内容
        return age == student.age &&
                Double.compare(student.score, score) == 0 &&
                Objects.equals(name, student.name) &&
                Objects.equals(sex, student.sex);
    }


}
class MyObject{
    public  boolean equals(Object  obj ){
        return  (this == obj);//Objct默认的实现方法
    }
}